package com.shandong.shandonglive.recommend.focus;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

/**
 * 为了兼容4.3以下版本的 AnimBridge. <br>
 * 使用方法： MainUpView.setAnimBridge(new AnimNoDrawBridge()); <br>
 * 如果边框带了阴影效果，使用这个函数自行调整: MainUpView.setDrawUpRectPadding(-12);
 *
 * @author hailongqiu
 */
public class EffectNoDrawBridge extends OpenEffectBridge {
    protected AnimatorSet mCurrentAnimatorSet;

    /**
     * 设置背景，边框不使用绘制.
     */
    @Override
    public void setUpRectResource(int resId) {
        getMainUpView().setBackgroundResource(resId);
    }

    @Override
    public void setUpRectDrawable(Drawable upRectDrawable) {
        getMainUpView().setBackgroundDrawable(upRectDrawable);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onOldFocusView(View oldFocusView, float scaleX, float scaleY) {
        if (!isAnimEnabled())
            return;
        if (oldFocusView != null) {
            oldFocusView.animate().scaleX(scaleX).scaleY(scaleY).setDuration(getTranDurAnimTime()).start();
        }
    }

    @Override
    public void onFocusView(View focusView, float scaleX, float scaleY) {
        if (!isAnimEnabled())
            return;
        if (focusView != null) {
            /**
             * 我这里重写了onFocusView. <br>
             * 并且交换了位置. <br>
             * 你可以写自己的动画效果. <br>
             */
            runTranslateAnimation(focusView, scaleX, scaleY);
            focusView.animate().scaleX(scaleX).scaleY(scaleY).setDuration(getTranDurAnimTime()).start();
        }
    }

    @Override
    public void onFocusViewRoot(View focusView, float scaleX, float scaleY, View rootView){
        if (!isAnimEnabled())
            return;
        if (focusView != null) {
            /**
             * 我这里重写了onFocusView. <br>
             * 并且交换了位置. <br>
             * 你可以写自己的动画效果. <br>
             */
            runTranslateAnimationRoot(focusView, scaleX, scaleY,rootView);
            focusView.animate().scaleX(scaleX).scaleY(scaleY).setDuration(getTranDurAnimTime()).start();
        }
    }

    /**
     * 重写边框移动函数.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void flyWhiteBorder(final View focusView, final View moveView, float scaleX, float scaleY) {
        Rect paddingRect = getDrawUpRect();
        int newWidth = 0;
        int newHeight = 0;
        int oldWidth = 0;
        int oldHeight = 0;

        int newX = 0;
        int newY = 0;

        if (focusView != null) {
            newWidth = (int) (focusView.getMeasuredWidth() * scaleX);
            newHeight = (int) (focusView.getMeasuredHeight() * scaleY);
            oldWidth = moveView.getMeasuredWidth();
            oldHeight = moveView.getMeasuredHeight();
            Rect fromRect = findLocationWithView(moveView);
            Rect toRect = findLocationWithView(focusView);
            int x = toRect.left - fromRect.left - (paddingRect.left);
            int y = toRect.top - fromRect.top - (paddingRect.top);
            newX = x - Math.abs(focusView.getMeasuredWidth() - newWidth) / 2;
            newY = y - Math.abs(focusView.getMeasuredHeight() - newHeight) / 2;
            //
            newWidth += (paddingRect.right + paddingRect.left);
            newHeight += (paddingRect.bottom + paddingRect.top);
        }

        // 取消之前的动画.
        if (mCurrentAnimatorSet != null)
            mCurrentAnimatorSet.cancel();

        ObjectAnimator transAnimatorX = ObjectAnimator.ofFloat(moveView, "translationX", newX);
        ObjectAnimator transAnimatorY = ObjectAnimator.ofFloat(moveView, "translationY", newY);
        // BUG，因为缩放会造成图片失真(拉伸).
        // hailong.qiu 2016.02.26 修复 :)
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofInt(new BaseEffectBridgeWrapper.ScaleView(moveView), "width", oldWidth,
                (int) newWidth);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofInt(new BaseEffectBridgeWrapper.ScaleView(moveView), "height", oldHeight,
                (int) newHeight);
        //
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(transAnimatorX, transAnimatorY, scaleXAnimator, scaleYAnimator);
        mAnimatorSet.setInterpolator(new DecelerateInterpolator(1));
        mAnimatorSet.setDuration(getTranDurAnimTime());
        mAnimatorSet.addListener(new AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (isVisibleWidget()) {
                    getMainUpView().setVisibility(View.GONE);
                }
                if (getNewAnimatorListener() != null)
                    getNewAnimatorListener().onAnimationStart(EffectNoDrawBridge.this, focusView, animation);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                getMainUpView().setVisibility(isVisibleWidget() ? View.GONE : View.VISIBLE);
                if (getNewAnimatorListener() != null)
                    getNewAnimatorListener().onAnimationEnd(EffectNoDrawBridge.this, focusView, animation);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }
        });
        mAnimatorSet.start();
        mCurrentAnimatorSet = mAnimatorSet;
    }

    //允许超出父View层级范围移动
    //@param rootView 移动参照的View
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void flyWhiteBorderRoot(final View focusView, View moveView, float scaleX, float scaleY, View rootView) {
        Rect paddingRect = getDrawUpRect();
        int newWidth = 0;
        int newHeight = 0;
        int oldWidth = 0;
        int oldHeight = 0;

        int newX = 0;
        int newY = 0;

        if (focusView != null) {
            newWidth = (int) (focusView.getMeasuredWidth() * scaleX);
            newHeight = (int) (focusView.getMeasuredHeight() * scaleY);
            oldWidth = moveView.getMeasuredWidth();
            oldHeight = moveView.getMeasuredHeight();
            Rect fromRect = findLocationWithView(moveView,rootView);
            Rect toRect = findLocationWithView(focusView,rootView);
            int x = toRect.left - fromRect.left - (paddingRect.left);
            int y = toRect.top - fromRect.top - (paddingRect.top);
            newX = x - Math.abs(focusView.getMeasuredWidth() - newWidth) / 2;
            newY = y - Math.abs(focusView.getMeasuredHeight() - newHeight) / 2;
            //
            newWidth += (paddingRect.right + paddingRect.left);
            newHeight += (paddingRect.bottom + paddingRect.top);
        }
        // 取消之前的动画.
        if (mCurrentAnimatorSet != null)
            mCurrentAnimatorSet.cancel();

        ObjectAnimator transAnimatorX = ObjectAnimator.ofFloat(moveView, "translationX", newX);
        ObjectAnimator transAnimatorY = ObjectAnimator.ofFloat(moveView, "translationY", newY);
        // BUG，因为缩放会造成图片失真(拉伸).
        // hailong.qiu 2016.02.26 修复 :)
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofInt(new BaseEffectBridgeWrapper.ScaleView(moveView), "width", oldWidth,
                (int) newWidth);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofInt(new BaseEffectBridgeWrapper.ScaleView(moveView), "height", oldHeight,
                (int) newHeight);
        //
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(transAnimatorX, transAnimatorY, scaleXAnimator, scaleYAnimator);
        mAnimatorSet.setInterpolator(new DecelerateInterpolator(1));
        mAnimatorSet.setDuration(getTranDurAnimTime());
        mAnimatorSet.addListener(new AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (isVisibleWidget()) {
                    getMainUpView().setVisibility(View.GONE);
                }
                if (getNewAnimatorListener() != null)
                    getNewAnimatorListener().onAnimationStart(EffectNoDrawBridge.this, focusView, animation);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                getMainUpView().setVisibility(isVisibleWidget() ? View.GONE : View.VISIBLE);
                if (getNewAnimatorListener() != null)
                    getNewAnimatorListener().onAnimationEnd(EffectNoDrawBridge.this, focusView, animation);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }
        });
        mAnimatorSet.start();
        mCurrentAnimatorSet = mAnimatorSet;
    }

    /**
     * 重写该函数，<br>
     * 不进行绘制 边框和阴影.
     */
    @Override
    public boolean onDrawMainUpView(Canvas canvas) {
        return false;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void setFocusChildrenMove(final View innerView, float innerScaleX, float innerScaleY, float moveX, float moveY) {
        if (!isAnimEnabled())
            return;
        //内部view先放大再移动
        if (innerView != null) {
            int oldWidth = innerView.getMeasuredWidth();
            int oldHeight = innerView.getMeasuredHeight();
            int newWidth = (int) (innerView.getMeasuredWidth() * innerScaleX);
            int newHeight = (int) (innerView.getMeasuredHeight() * innerScaleY);
            float newX = (newWidth - oldWidth)/2  + moveX;
            float newY = (newHeight - oldHeight)/2  + moveY;
            //if (mCurrentAnimatorSet != null)
            //    mCurrentAnimatorSet.cancel();
            //ObjectAnimator scaleXAnimator = ObjectAnimator.ofInt(new ScaleView(innerView),"width",oldWidth,newWidth);
            //ObjectAnimator scaleYAnimator = ObjectAnimator.ofInt(new ScaleView(innerView),"height",oldHeight,newHeight);
            ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(innerView, "scaleX", innerScaleX);
            ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(innerView, "scaleY", innerScaleY);
            ObjectAnimator transXAnimator = ObjectAnimator.ofFloat(innerView, "translationX", newX);
            ObjectAnimator transYAnimator = ObjectAnimator.ofFloat(innerView, "translationY", -newY);
//            scaleXAnimator.setDuration(getTranDurAnimTime());
//            scaleYAnimator.setDuration(getTranDurAnimTime());
//            transXAnimator.setDuration(getTranDurAnimTime());
//            transYAnimator.setDuration(getTranDurAnimTime());
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(scaleXAnimator, scaleYAnimator, transXAnimator, transYAnimator);
            animatorSet.setInterpolator(new DecelerateInterpolator(1));
            animatorSet.setDuration(getTranDurAnimTime()*2);
            animatorSet.start();
            mCurrentAnimatorSet = animatorSet;
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void setUnFocusChildrenMove(final View innerView, float innerScaleX, float innerScaleY, float moveX, float moveY) {
        if (!isAnimEnabled())
            return;
        if (innerView != null) {
            int oldWidth = innerView.getMeasuredWidth();
            int oldHeight = innerView.getMeasuredHeight();
            int newWidth = (int) (innerView.getMeasuredWidth() * innerScaleX);
            int newHeight = (int) (innerView.getMeasuredHeight() * innerScaleY);
            final float newX =(oldWidth - newWidth)/2  + moveX;
            final float newY = (oldHeight - newHeight)/2  + moveY;
            //和飞框动画同时使用需要注释取消部分，防止动画不执行
            //if (mCurrentAnimatorSet != null)
            //    mCurrentAnimatorSet.cancel();
            //ObjectAnimator scaleXAnimator = ObjectAnimator.ofInt(new ScaleView(innerView),"width",oldWidth,newWidth);
            //ObjectAnimator scaleYAnimator = ObjectAnimator.ofInt(new ScaleView(innerView),"height",oldHeight,newHeight);
            ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(innerView, "scaleX", 1);
            ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(innerView, "scaleY", 1);
            ObjectAnimator transXAnimator = ObjectAnimator.ofFloat(innerView, "translationX", moveX);
            ObjectAnimator transYAnimator = ObjectAnimator.ofFloat(innerView, "translationY", - newY);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(scaleXAnimator, scaleYAnimator, transXAnimator, transYAnimator);
            animatorSet.setInterpolator(new DecelerateInterpolator(1));
            animatorSet.setDuration(getTranDurAnimTime()*2);
            animatorSet.start();
            mCurrentAnimatorSet = animatorSet;
        }
    }
}
